package com.example.ymm.modules1.service.impl.hl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.yw.YwUser;
import com.example.ymm.modules1.entity.ms.DEmployee;
import com.example.ymm.modules1.mapper.hl.YwUserMapper;
import com.example.ymm.modules1.query.hl.YwUserQueryCriteria;
import com.example.ymm.modules1.service.hl.YwUserService;
import com.example.ymm.modules1.service.ms.DEmployeeService;
import com.example.ymm.modules1.service.xft.XftService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@DS("slave2")
public class YwUserServiceImpl extends BaseServiceImpl<YwUserMapper, YwUser> implements YwUserService {


    @Autowired
    XftService xftService;
    @Autowired
    DEmployeeService dEmployeeService;

    @Override
    public TableList<YwUser> list(YwUserQueryCriteria criteria, YmPageDto pageDto) {
        getPage(pageDto);
        List<YwUser> arrs = queryAll(criteria);
        PageInfo<YwUser> pageInfo = new PageInfo<>(arrs);
        TableList<YwUser> tableList = new TableList(pageInfo);
        return tableList;
    }

    public List<YwUser> queryAll(YwUserQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(YwUser.class, criteria);
        predicate.orderByDesc("create_time");
        List<YwUser> result = baseMapper.selectList(predicate);
        return result;
    }

    public YwUser getUserByName(String userName) {
        YwUserQueryCriteria criteria=new YwUserQueryCriteria();
        criteria.setUserName(userName);
        List<YwUser> ywUsers = queryAll(criteria);
        if(ywUsers.size()>0){
            return ywUsers.get(0);
        }
        return null;
    }


    @Override
    public YwUser getMemberId(String userName) {
        String userNo=null;
        //userName=getUserName();
        YwUser user = getUserByName(userName);

        String memberId = null;
        if(UtilValidate.isNotEmpty(user)){
            return user;
        }else{
            // 动态切换数据源
            DynamicDataSourceContextHolder.push("slave");
            userNo=dEmployeeService.getTidByName(userName);
            DynamicDataSourceContextHolder.clear();
            DynamicDataSourceContextHolder.push("slave2");

            JSONObject memberInfo = xftService.getMemberInfoByUserName(userName);
            if(UtilValidate.isNotEmpty(memberInfo)){
                memberId = memberInfo.getString("memberId");
                YwUser n_user =new YwUser();
                n_user.setMemberId(memberId);
                n_user.setUserName(userName);
                n_user.setUserNo(userNo);
                saveOrUpdate(n_user);
                return n_user;
            }
        }
        return null;
    }

    @Override
    public DEmployee getUserInfoToSlave2(String userName) {
        DynamicDataSourceContextHolder.push("slave");
        DEmployee dEmployee=dEmployeeService.getUserInfo(userName);
        DynamicDataSourceContextHolder.clear();
        DynamicDataSourceContextHolder.push("slave2");
        return dEmployee;
    }
}
